﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.ViewModels.Configuration.DataGrid;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigDrugReasonsCtrlViewModel : BaseViewModel<ConfigDrugReasonsCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";

        private ObservableCollection<ConfigDrugReasonsModel> _displayConfigDrugReasonsModel = new ObservableCollection<ConfigDrugReasonsModel>();

        #endregion

        #region Constructor

        public ConfigDrugReasonsCtrlViewModel()
        { }



        #endregion

        #region Properties



        public ObservableCollection<ConfigDrugReasonsModel> DisplayConfigDrugReasonsModel
        {
            get
            {
                _displayConfigDrugReasonsModel.Clear();
                List<ConfigDrugReasonsModel> lst = ConfigDrugReasonsDataGridViewModel.Instance.ConfigDrugReasonsModel.Where(O =>
                O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigDrugReasonsModel.Add(item);
                }
                return _displayConfigDrugReasonsModel;
            }

            set
            {
                ConfigDrugReasonsDataGridViewModel.Instance.ConfigDrugReasonsModel = value;
                RaisePropertyChanged("DisplayConfigDrugReasonsModel");
            }
        }

        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigDrugReasonsModel");
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigDrugReasonsModelService.Instance.Update(sender as ConfigDrugReasonsModel);
        }
        private void AddConfigDrugReasonsModel(ConfigDrugReasonsModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigDrugReasonsModelService.Instance.Update(obj);
                ConfigDrugReasonsDataGridViewModel.Instance.ConfigDrugReasonsModel.Add(obj);
            }
            else ConfigDrugReasonsModelService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigDrugReasonsModel(ConfigDrugReasonsModel configDrugReasonsModel)
        {
            if (configDrugReasonsModel != null)
            {
                ConfigDrugReasonsDataGridViewModel.Instance.ConfigDrugReasonsModel.Remove(configDrugReasonsModel);
                RaisePropertyChanged("DisplayConfigDrugMethodsModels");
                ConfigDrugReasonsModelService.Instance.DeleteById(configDrugReasonsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigDrugReasonsModel model = new ConfigDrugReasonsModel();
            DisplayConfigDrugReasonsModel.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }

        #endregion

        #region EventHandlers

        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigDrugReasonsModel(sender as ConfigDrugReasonsModel);
        }
        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion

    }
}
